<?php
include_once LIB_PTH.'PHPMailer/PHPMailer.class.php';

class Mail extends MyBase{
	
	private $oPHPMailer;
	private $T;
	private $setting;
	private $content;
	private $type;
	private $vars;
	private $date;
	private $time;
	
	function __construct($setting = array(), $send_type = 'smtp'){
		parent::__construct('queue.id');
		/*if(empty($setting)){
			$setting = Statics::$mail;	// 默认配置
		}*/
		$this->setting		= $setting;
		$this->date			= fdate(0, true);
		$this->time			= TIME;
		$this->oPHPMailer	= new PHPMailer();
		if($send_type == 'smtp'){
			$this->smtp_init();
		}
		$this->init();

		// 模板
		include APP_PTH.'_tpl.php';
		$this->T = $T;
	}
	
	public function init(){
		$this->oPHPMailer->CharSet	= 'utf-8';
		$this->oPHPMailer->Host		= $this->setting['host'];		// 必填，设置SMTP服务器
		$this->oPHPMailer->Port		= $this->setting['port'];		// 设置端口
		
		$this->oPHPMailer->Username	= $this->setting['from'];		// 必填，开通SMTP服务的邮箱；任意一个163邮箱均可
		$this->oPHPMailer->Password	= $this->setting['pass'];		// 必填， 以上邮箱对应的密码
		
		$this->oPHPMailer->From		= $this->setting['from'];		// 必填，发件人Email
		$this->oPHPMailer->FromName	= $this->setting['fromName'];	// 必填，发件人昵称或姓名
		
		$this->oPHPMailer->AddReplyTo($this->setting['replyto'], $this->setting['fromName']);	// 回复邮箱地址
	}
	
	public function smtp_init(){
		$this->oPHPMailer->IsSMTP();
		$this->oPHPMailer->SMTPAuth	= true;	// 必填，SMTP服务器是否需要验证，true为需要，false为不需要
	}
	
	public function get_err_info(){
		$this->oPHPMailer->ErrorInfo;
	}
	
	public function send($type, $toUser, $toName, $vars = array(), $resend = false){
		// 判断本次是否已经发送过
		$sign = $this->make_sign($type, $toUser);
		$sent = $this->get_from_queue($sign, 'id');
		if($resend == false && !empty($sent)){
			//return '已发送过相同邮件';
			return 'succ';
		}
		
		$this->oPHPMailer->AddAddress($toUser, $toName);	// 收件人地址。参数一：收信人的邮箱地址，可添加多个。参数二：收件人称呼
		$this->oPHPMailer->IsHTML(true);					// 是否以HTML形式发送，如果不是，请删除此行
		$this->oPHPMailer->WordWrap = 0;					// 自动换行的字数

		$this->vars = array(
			'type'		=> $type,
			'sign'		=> $sign,
			'readback'	=> $this->get_readback_url($sign),
			'email'		=> $toUser,
			'username'	=> $toName,
			//'subject'	=> '',
			//'altbody'	=> '',
			//'content'	=> '',
		);
		$this->vars = array_merge($this->vars, $vars);
		
		$this->get_content($type);
		$r = $this->oPHPMailer->Send();
		if($r){	// 入队列
			if(!empty($sent) || $this->add_to_queue($sign, $type, $toUser, $toName)){
				return 'succ';
			}
			return '入库失败';
		}
		return $this->get_err_info();
	}

	public function set_wrap($wrap){
		$this->oPHPMailer->WordWrap = $wrap;				// 自动换行的字数
	}

	public function set_ishtml($ishtml){
		$this->oPHPMailer->IsHTML($isHtml);					// 是否以HTML形式发送，如果不是，请删除此行
	}
	
	public function get_content($type){
		/**
		 * 特别注意，标题长度不能太长，否则会收不到
		 */
		set_template_path($this->T, THR_PTH.'_mail/');
		// 模板赋值
		if(empty($__l)){$__l = array('site_name' => $_SERVER['HTTP_HOST'], 'site_description' => '', 'site_keywords' => '', ); }
		$_s = array(
			'page_title'		=> lang('site_name'),
			'page_description'	=> lang('site_description'),
			'page_keywords'		=> lang('site_keywords'),
			// 其他
			'web_url'			=> WEB_URL,
			'pub_url'			=> rtrim(WEB_URL, '/').PUB_DIR,
			'img_url'			=> rtrim(WEB_URL, '/').IMG_DIR,
			'up_url'			=> UP_URL,
		);
		$this->T->assign('theme_dir', THR_PTH.'_mail/');
		$this->T->assign('vars', $this->vars);
		$this->T->assign('_s', $_s);

		if(!empty($this->vars['subject'])){
			$this->set_subject($this->vars['subject']);
		}
		if(!empty($this->vars['altbody'])){		// 纯文本展现时的内容，一般用不到
			$this->set_altbody($this->vars['altbody']);
		}
		if(!empty($this->vars['content'])){
			$this->set_content($this->vars['content']);
		}else{
			$this->get_html($type);
		}
		
	}
	
	/********************************************************************************
	 * 主题内容处理
	 ********************************************************************************/

	public function set_subject($subject){
		$this->oPHPMailer->Subject = $subject;
		$this->oPHPMailer->Subject = String::csubstr($this->oPHPMailer->Subject, 72);
	}

	public function set_altbody($altbody){
		$this->oPHPMailer->AltBody = $altbody;
	}

	public function set_content($content){
		$this->content = eregi_replace("[\]", '', $content);
		//echo $this->content;exit;
		$this->oPHPMailer->MsgHTML($this->content);
	}

	public function get_html($type){
		if(!file_exists(THR_PTH."_mail/{$type}.html")){
			echo 'no content, and<br />';
			echo THR_PTH."_mail/{$type}.html doesn't exist!!!";
			exit;
		}
		$content = $this->T->fetch("{$type}.html", null, null, false);
		$this->set_content($content);
	}
		
	/********************************************************************************
	 * 队列
	 ********************************************************************************/
	
	public function make_sign($type, $email){
		return md5($type.$email.$this->date);
	}
	
	public function get_readback_url($sign){
		//return 'http://iegole.com/pub/readback/'.$sign.'.jpg';
		return WEB_URL.'pub/readback/'.$sign.'.jpg';
	}
	
	public function get_from_queue($sign, $fields = '*'){
		return $this->db->get_row($this->tbname, array('sign' => $sign), $fields);
	}
	
	public function add_to_queue($sign, $type, $email, $nickname){
		$data = array(
			'sign'		=> $sign,
			'type'		=> $type,
			'email'		=> $email,
			'nickname'	=> $nickname,
			'date'		=> $this->date,
			'addtime'	=> $this->time,
		);
		return $this->db->insert($this->tbname, $data);
	}
	
	public function read($sign){
		$queue = self::get_from_queue($sign, '`read`');
		$data = array(
			'read'		=> intval($queue['read']) + 1,
			'readtime'	=> TIME,
		);
		return $this->db->update($this->tbname, array('sign' => $sign), $data);
	}
}
?>